//
//  DetailViewController.m
//  iMPushTestDemo
//
//  Created by xuyingwei on 16/8/10.
//  Copyright © 2016年 xuyingwei. All rights reserved.
//
#define leftDistance 32/2
#define rightDistance 36/2
#define titleFontSize 42/2
#define dateFontSize 30/2
#define nameFontSize 30/2
#define messageFontSize 30/2
#define titletextColor 0x111111
#define datetextColor 0x858585
#define nametextColor 0x4ca5ff
#define messagetextColor 0x858585
#define lineColor 0xd2d2d2


#import "DetailViewController.h"
#import "Masonry.h"
#import "NetRequest.h"
#import "KeyValuesClass.h"

@interface DetailViewController ()
@property (nonatomic, strong)NSString *NoInformationStr;
@end

@implementation DetailViewController
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [super viewWillAppear:YES];
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"图层-6"] style:UIBarButtonItemStylePlain target:self action:@selector(rebackToRootViewAction)];
        [self createPushLeftNavBar];
        
//#ifdef Main
        if([getUIStyle() isEqualToString:STYLE0]){
        NSLog(@"主项目程序");
         [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav3"] forBarMetrics:UIBarMetricsDefault];
        }else{
//#elif Student
        
        NSLog(@"学员端程序");
         [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"redbar"] forBarMetrics:UIBarMetricsDefault];
        }
        //    [self dataRequest];
        
        //    return YES;
        
//#elif Teacher
//        //    rootview
//        NSLog(@"老师端程序");
//#else
//        
//        NSLog(@"都不是");
//#endif
       
    }else{
//        self.navigationItem.leftBarButtonItem=nil;
        [self createLeftNavBar];
    }
}

-(NSString *)NoInformationStr{
    if (!_NoInformationStr) {
        //这里添加功能判断
        if (getEnglishLanguage()) {
            _NoInformationStr = @"Not available";
        }else{
            _NoInformationStr = @"暂无";
        }

    }
    return _NoInformationStr;
}
- (void)returnText:(ReturnTextBlock)block {
    self.returnTextBlock = block;
}
- (void)viewWillDisappear:(BOOL)animated {
    if ([self.read isEqualToString:@"read"]) {
        [self.delegate showName:self.read andFlag:_isNeedRefresh];
        
    }else{
        [self.delegate showName:@"other" andFlag:_isNeedRefresh];
        _isNeedRefresh = NO;
    }
    
    
    if (self.returnTextBlock != nil) {
        self.returnTextBlock(@"read");
    }
    [super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    _isNeedRefresh = YES;
//    [self setTitle:@"消息公告"];
//    [self ];
    UILabel *titleLabel = [[UILabel alloc] init];
    self.navigationItem.titleView = titleLabel;
    titleLabel.frame = CGRectMake(0, 0, 200, 44);
    titleLabel.text = @"消息公告";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:36/2];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self createUI];
    [self dataRequest];
    
    
    
    // Do any additional setup after loading the view.
}
-(void)createPushLeftNavBar{
    UIButton *leftNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftNavBtn.frame = CGRectMake(0, 0, 11, 37/2);
    [leftNavBtn setBackgroundImage:[UIImage imageNamed:@"形状1-19-拷贝-4"] forState:UIControlStateNormal];
    [leftNavBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:leftNavBtn];
    self.navigationItem.leftBarButtonItem = item;
    
}

- (void)createUI{
    
#pragma mark - testUI begain
//    self.view.backgroundColor = [UIColor colorWithRed:(135/255.0f) green:(109/255.0f) blue:(175/255.0f) alpha:1];
    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeSystem];
    mybutton.frame = CGRectMake(50, 150, 200, 50);
    [self.view addSubview:mybutton];
    
    if (_message) {
        [mybutton setTitle:@"返回" forState:UIControlStateNormal];
    }else{
        [mybutton setTitle:@"返回" forState:UIControlStateNormal];
    }
    
    UILabel *lab = [[UILabel alloc] init];
    lab.numberOfLines = 0;
    [lab sizeToFit];
    [self.view addSubview:lab];
    lab.lineBreakMode = NSLineBreakByCharWrapping;
    //    [lab sizeToFit];
    //    lab.text = @"clkjaf";
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mybutton.mas_bottom).offset(50);
        make.left.equalTo(mybutton.mas_left);
        make.width.equalTo(@(200));
    }];
    if (_message) {
        NSString * messageInfo = [[_message objectForKey:@"aps"] objectForKey:@"alert"];
        NSLog(@"%@",messageInfo);
        
        lab.text = messageInfo;
    }else{
        lab.text = @"无数据askdfjaksjdfljlasdlkfjlaksdjflkjaskldfjjsaldjfkajsdjfkasjdlfjasjkdfljklasjlfaskdjfjaskdljfkjaskldfjljasdfjlasjdjfjsldjfkjlasjdkfjlas";
    }
    
    lab.backgroundColor = [UIColor brownColor];
    lab.textColor = [UIColor lightGrayColor];
    [mybutton setBackgroundColor:[UIColor lightTextColor]];
    [mybutton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [mybutton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    mybutton.hidden = YES;
    lab.hidden = YES;
    
#pragma mark - testUI end
    UILabel * titleLable = [[UILabel alloc] init];//标题
    [self.view addSubview:titleLable];
    UILabel * messageLabel = [[UILabel alloc] init];//推送内容
    [self.view addSubview:messageLabel];
    UILabel *messageData = [[UILabel alloc] init];//推送时间
    [self.view addSubview:messageData];
    UILabel *ifseeLabel = [[UILabel alloc] init];//查看状态
    [self.view addSubview:ifseeLabel];
    UILabel *name = [[UILabel alloc] init];//人员姓名
    [self.view addSubview:name];
    UIView *lineView = [[UIView alloc] init];//分割线
    [self.view addSubview:lineView];
    
    
    
    [titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(60/2);
        make.left.equalTo(self.view.mas_left).offset(leftDistance);
        make.right.equalTo(self.view.mas_right).offset(-rightDistance);
//        make.height.equalTo(@(20));
    }];
    
    
    [messageData mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLable.mas_bottom).offset(56/2);
        make.left.equalTo(titleLable.mas_left);
        make.height.equalTo(@(30/2));
    }];
    
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageData.mas_top);
        make.left.equalTo(messageData.mas_right).offset(20);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(1));
        make.left.equalTo(titleLable.mas_left);
        make.right.equalTo(titleLable.mas_right);
        make.top.equalTo(messageData.mas_bottom).offset(42/2);
        
    }];
    
    
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(58/2);
        make.left.equalTo(titleLable.mas_left);
        make.right.equalTo(titleLable.mas_right);
    }];
    
    
    
    [ifseeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageData.mas_top);
        make.left.equalTo(messageLabel.mas_left);
    }];
    
    
    if (self.titleName) {
        titleLable.text = self.titleName;
        
    }else{
        if (self.model.title) {
            titleLable.text = self.model.title;
        }else{
            titleLable.text = @"暂无";
        }
    }
    
//    titleLable.text = self.model.title;
    titleLable.font = [UIFont systemFontOfSize:titleFontSize];
    titleLable.textColor = [UIColor colorWithHex:titletextColor];
    titleLable.textAlignment = NSTextAlignmentLeft;
    titleLable.numberOfLines = 0;
    titleLable.lineBreakMode = NSLineBreakByCharWrapping;
    [titleLable sizeToFit];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleLable.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle setLineSpacing:18/2];//调整行间距
    
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [titleLable.text length])];
    //    _labelMessageDetail.text = @"";
    titleLable.attributedText = attributedString;
    //    [contentView addSubview:_labelMessageDetail];
//    [titleLable sizeToFit];

    
    if (self.createDate) {
        messageData.text = self.createDate;
    }else{
        if (self.model.create_date) {
//            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:SS"];
            
//            NSString *dateLoca = [NSString stringWithFormat:@"%@",self.model.create_date];
//            NSTimeInterval time=[dateLoca doubleValue]/1000.0;
//            NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//            NSDate *dateBJTime = [[NSDate alloc]initWithTimeInterval:8*60*60 sinceDate:detaildate];
            
            
            NSString *destDateString = [KeyValuesClass dateGlinToFormate:[NSString stringWithFormat:@"%@", _model.create_date] andFromate:nil]; //[formatter stringFromDate:dateBJTime];
            
            
            messageData.text = [NSString stringWithFormat:@"%@", destDateString];
            
            [messageData sizeToFit];
            //    messageData.text = self.model.create_date;
            messageData.font = [UIFont systemFontOfSize:12];
            messageData.textColor = [UIColor colorWithHex:0x333333];
        }else{
            messageData.text = self.NoInformationStr;
            NSLog(@"%@",_NoInformationStr);
        }
        
    }
    messageData.font = [UIFont systemFontOfSize:dateFontSize];
    messageData.textColor = [UIColor colorWithHex:datetextColor];
    
    
    if (self.noticeName) {
        name.text = self.noticeName;
    }else{
        if (self.model.notice_name) {
            name.text = self.model.notice_name;
        }else{
            name.text = self.NoInformationStr;
        }
    }
    name.font = [UIFont systemFontOfSize:nameFontSize];
    name.textColor = [UIColor colorWithHex:nametextColor];
    [name sizeToFit];
    
    
    lineView.backgroundColor = [UIColor colorWithHex:lineColor];
    
    
    
    
    NSString * messageInfo = [[_message objectForKey:@"aps"] objectForKey:@"alert"];
    NSLog(@"%@",messageInfo);
//    if (messageInfo) {
//        messageLabel.text = messageInfo;
//        titleLable.text = self.titleName;
//    }else{
//        messageLabel.text = self.model.content;
//        titleLable.text = self.model.title;
//    }
    if (messageInfo) {
        messageLabel.text = messageInfo;
    }else{
        if (self.model.content) {
            messageLabel.text = self.model.content;
        }else{
            messageLabel.text = self.NoInformationStr;;
        }
    }
    
    
    messageLabel.font = [UIFont systemFontOfSize:messageFontSize];
    messageLabel.numberOfLines = 0;
    [messageLabel sizeToFit];
    messageLabel.textColor = [UIColor colorWithHex:messagetextColor];
    messageLabel.lineBreakMode = NSLineBreakByCharWrapping;
    NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:messageLabel.text];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle1 setLineSpacing:30/2];//调整行间距
    
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [messageLabel.text length])];
    //    _labelMessageDetail.text = @"";
    messageLabel.attributedText = attributedString1;

    
    
    
    
    ifseeLabel.text = [NSString stringWithFormat:@"是否查看：是"];
    ifseeLabel.textColor = [UIColor colorWithHex:0x333333];
    ifseeLabel.font = [UIFont systemFontOfSize:12];
    NSLog(@"%zd",self.model.mainId.intValue);
    
    ifseeLabel.hidden = YES;//查看状态暂时不需要，隐藏掉
    
}
//-(void)LeftClickDetail:(UIButton *)btn{
//    [self.navigationController popViewControllerAnimated:YES];
//}
- (void)rebackToRootViewAction {
    NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults];
    [pushJudge setObject:@""forKey:@"push"];
    [pushJudge synchronize];//记得立即同步
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)dataRequest{

    if ([self.model.if_seeCn isEqualToString:@"是"]) {
        return;
    }
    
    NSMutableDictionary *paramas = [[NSMutableDictionary alloc] init];
    [paramas setObject:@"SYSTEM_MESSAGE" forKey:@"msgTabName"];
    if (self.mainId) {
        [paramas setObject:self.mainId forKey:@"msgMainId"];
    }else{
        if (![self.model.mainId isEqualToString:@"NULL"]) {
            [paramas setObject:self.model.mainId forKey:@"msgMainId"];
        }else{
            [paramas setObject:@"" forKey:@"msgMainId"];
        }
    }
    
    [paramas setObject: getSessionId() forKey:@"sessionId"];
    
    NSLog(@"%@",paramas);
    NSString *requestStr = [NSString stringWithFormat:@"%@%@", getBase(), MESSAGEDETAIL];
    [NetRequest requestUrl:requestStr andParamas:paramas andReturnBlock:^(NSData *data, NSError *error) {
        
        if (data!=nil&&!error) {
            id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",obj);
            
            if ([obj valueForKey: @"promptMessage"]) {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"relogin" object:@"relogin"];
                
            }
            
            
        }else{
            NSString *str = [NSString stringWithFormat:@"%@", error.localizedDescription];
            NSLog(@"error message:%@",str);
//            [self showLabelWithText:str];
        }

        
    }];

}
- (void)btnClick:(UIButton *)sender
{
    
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    [pushJudge setObject:@"" forKey:@"push"];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
